package com.longxiao.validator.parser;


import com.longxiao.validator.ValidResult;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * Created by houhongwei on 2017/5/12.
 */
public abstract class Parser {

    /**
     * 执行规则验证
     * @param anno
     * @param value
     * @return
     */
    public ValidResult doParser(Annotation anno, Object value) {
        if (isNull(value)) {
            if (isRequired(anno) == Boolean.FALSE) {
                // 非必填参数，没有天填写直接返回成功
                return ValidResult.createSucc();
            } else {
                return ValidResult.createFail(getErr(anno));
            }
        }
        return doValid(value, anno);
    }

    protected abstract ValidResult doValid(Object value, Annotation annotation);

    /**
     * 获得注解对象class
     * @return
     */
    public abstract Class getAnno();

    /**
     * 是否为必要参数
     * @param annotation
     * @return
     */
    protected abstract boolean isRequired(Annotation annotation);

    /**
     * 获得错误提示
     * @param annotation
     * @return
     */
    protected abstract String getErr(Annotation annotation);

    /**
     * 判断是否为NULL
     * @param value
     * @return
     */
    protected boolean isNull(Object value) {
        return (value == null ? Boolean.TRUE : Boolean.FALSE);
    }

}
